package controllers;

import java.io.IOException;
import java.sql.SQLException;

import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import views.LoginView;
import views.TextListenerFactory;
import workspace.Workspace;
import dialogs.LoginDialog;

public class LoginController {

	private String felhasznaloNev;
	private String jelszo;
	private final LoginDialog loginDialog;
	private Workspace workspace;

	public LoginController(final LoginDialog loginDialog) {
		this.loginDialog = loginDialog;
		final LoginView loginView = loginDialog.getLoginView();
		loginView.getBtBelepes().addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				try {
					tryLogin();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		loginView.getTxFelhasznaloNev().addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent arg0) {
				if (arg0.keyCode == 13) {
					try {
						tryLogin();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		});

		loginView.getTxJelszo().addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent arg0) {
				if (arg0.keyCode == 13) {
					try {
						tryLogin();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		});
		loginView.getTxJelszo().addKeyListener(
				TextListenerFactory.passwordKeyListener());
		loginView.getTxJelszo().addModifyListener(
				TextListenerFactory.szovegHosszListener(30));
		loginView.getTxFelhasznaloNev().addKeyListener(
				TextListenerFactory.userNameKeyListener());
		loginView.getTxFelhasznaloNev().addModifyListener(
				TextListenerFactory.szovegHosszListener(20));

	}

	private void tryLogin() throws IOException {
		LoginView loginView = loginDialog.getLoginView();
		felhasznaloNev = loginView.getTxFelhasznaloNev().getText();
		jelszo = loginView.getTxJelszo().getText();
		loginView.getLabHibauzenet().setText("");
		try {
			if (loginDialog.getConnectionString() != null) {
				workspace = Workspace.getWorkspace(felhasznaloNev, jelszo,
						loginDialog.getConnectionString());
			} else {
				workspace = Workspace.getWorkspace(felhasznaloNev, jelszo);
			}
		} catch (ClassNotFoundException e) {
			loginView.getLabHibauzenet().setText(
					"Hiba: " + e.getMessage() + "!");
		} catch (SQLException e) {
			loginView.getLabHibauzenet().setText(
					"Hiba: " + e.getMessage() + "!");
		}
		loginView.layout();
		if (workspace != null) {
			loginDialog.getShell().close();
		}
	}

	public String getFelhasznaloNev() {
		return felhasznaloNev;
	}

	public String getJelszo() {
		return jelszo;
	}

	public Workspace getWorkspace() {
		return workspace;
	}

}
